package com.pagepub.core.tag.validate;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.pagepub.core.utils.StringUtil;


/**
 * 验证数据类型函数
 * @author ggd
 */
public class PortalValidate
{
	private static Log log = LogFactory.getLog(PortalValidate.class);

	/**
	 * 判断str是否为""是返回true，否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str)
	{
		boolean retu = false;
		if (str != null && str.trim().length() == 0)
		{
			retu = true;
		}
		return retu;
	}

	/**
	 * 判断str是否是email格式，是返回true,否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmail(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^\\w+@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";// "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否是(中国式)电话格式,是返回true,否则返回false
	 * 
	 * @param str
	 * @return 例子：String
	 *         str="086-0596-2217011";//有效，086是中国代码，0596是福建漳州电话区号,2217011电话号码(可7-8位数)
	 */
	public static boolean isPhone(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^((\\(\\d{2,3}\\))|(\\d{3}\\-))?(\\(0\\d{2,3}\\)|0\\d{2,3}-)?[1-9]\\d{6,7}(\\-\\d{1,4})?$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否是手机号码格式，是返回true否则返回false(中国手机13开头的)
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isMobile(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^((\\(\\d{2,3}\\))|(\\d{3}\\-))?1\\d{10}$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否是url格式，是返回true否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isUrl(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		// 表达式有问题
		String parttenStr = "^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否是货币格式，是返回true否则返回false(小数点最大为4位)
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isCurrency(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^\\d+(\\.\\d{1,4})?$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否是数字，是返回true否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^\\d+$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 是否有字母

	 * @param str
	 * @return
	 */
	public static boolean hasChar(String str)
	{
		if (str == null || str.length() <= 0)
		{
			return false;
		}
		String parttenStr = "[A-Z]*[a-z]*";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否是邮政编码，是返回true否则返回false,(长度为6位的数字) 值为null或""时不校验
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isZip(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^[0-9]\\d{5}$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否是整数(可带正负号),是返回true否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isInteger(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^[-\\+]?\\d+$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否是单精度(可带正负号),是返回true否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isFloat(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^[-\\+]?\\d+(\\.\\d+)?$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否只有字母,是返回true否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEnglish(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^[A-Za-z]+$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否是中文,是返回true否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isChinese(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^[\\u0391-\\uFFE5]+$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否较为安全的密码串,是返回true否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isPwdSafe(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^(([A-Z]*|[a-z]*|\\d*|[-_\\~!@#\\$%\\^&\\*\\.\\(\\)\\[\\]\\{\\}<>\\?\\\\/\\\'\\\"]*)|.{0,5})$|\\s";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否是日期时间格式,是返回true否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isTime(String str)
	{
		if (str == null || str.length() <= 0)
			return false;
		String parttenStr = "^(20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$";
		CharSequence cs = str;
		Pattern pat = Pattern.compile(parttenStr, Pattern.MULTILINE);
		Matcher mc = pat.matcher(cs);
		return mc.find();
	}

	/**
	 * 判断str是否是日期格式(不含时间)
	 * 
	 * @param str
	 * @param formatStr
	 *            日期格式化模板如：yyyy-MM-dd HH:mm:ss
	 */
	public static boolean isDate(String str, String formatStr)
	{

		DateFormat fd = new SimpleDateFormat(formatStr);
		try
		{
			fd.parse(str);
		}
		catch (ParseException e)
		{
			log.error("格式化出错:MyValidate.isDate(\""+str+"\",\""+formatStr+"\")",e);
			return false;
		}
		return true;
	}

	/**
	 * 
	 * 判断str是否是日期格式(含时间)
	 * 
	 * @param str
	 * @param formatStr
	 *            只有这三个值"ymd","mdy","dmy"，大小写不区分

	 * @param splitstr
	 *            日期格式的分隔符如："/","-","."等

	 * @return 例子： String str="2005/01/01 20:11:23";
	 *         isDate(str,"ymd","/");//返回true
	 */

	/**
	 * str的长度是否在start与end之间，是返回true否则返回false,str==null返回false
	 * 
	 * @param str
	 * @param start
	 * @param end
	 * @return
	 */
	public static boolean isLengthScope(String str, int start, int end)
	{
		if (str == null || str.length() <= 0)
			return false;
		if (StringUtil.lengthB(str) >= start && StringUtil.lengthB(str) <= end)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * i的值是否在start与end之间，是返回true否则返回false
	 * 
	 * @param i
	 * @param start
	 * @param end
	 * @return
	 */
	public static boolean isIntScope(int i, int start, int end)
	{
		if (i >= start && i <= end)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * i的值是否在start与end之间，是返回true否则返回false
	 * 
	 * @param i
	 * @param start
	 * @param end
	 * @return
	 */
	public static boolean isFloatScope(float i, float start, float end)
	{
		if (i >= start && i <= end)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * str的长度是否超过maxlen,是返回true否则返回false
	 * 
	 * @param str
	 * @param maxlen
	 * @return
	 */
	public static boolean isGreatthanMaxLength(String str, int maxlen)
	{
		if (str == null || str.length() <= 0)
			return false;
		if (StringUtil.lengthB(str) > maxlen)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * str的长度是否低于minlen,是返回true否则返回false
	 * 
	 * @param str
	 * @param minlen
	 * @return
	 */
	public static boolean isLessthanMinLength(String str, int minlen)
	{
		if (str == null || str.length() <= 0)
			return false;
		if (StringUtil.lengthB(str) < minlen)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 判断i<min，是返回true否则返回false
	 * 
	 * @param i
	 * @param min
	 * @return
	 */
	public static boolean isLessthanMin(int i, int min)
	{
		if (i < min)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 判断i<min，是返回true否则返回false
	 * 
	 * @param i
	 * @param min
	 * @return
	 */
	public static boolean isLessthanMin(float i, float min)
	{
		if (i < min)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 判断i>max，是返回true否则返回false
	 * 
	 * @param i
	 * @param max
	 * @return
	 */
	public static boolean isGreatthanMax(int i, int max)
	{
		if (i > max)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 判断i>max，是返回true否则返回false
	 * 
	 * @param i
	 * @return
	 */
	public static boolean isGreatthanMax(float i, float max)
	{
		if (i > max)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 判断s日期大于d,是返回true否则返回false
	 * @param s
	 * @param d
	 * @return
	 */
	public static boolean greatDate(Date s, Date d)
	{
		if (s == null || d == null)
		{
			return false;
		}
		if (s.getTime() > d.getTime())
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 判断s日期大于等于d,是返回true否则返回false
	 * @param s
	 * @param d
	 * @return
	 */
	public static boolean greatEqualDate(Date s, Date d)
	{
		if (s == null || d == null)
		{
			return false;
		}
		if (s.getTime() >= d.getTime())
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 判断s日期小于d,是返回true否则返回false
	 * @param s
	 * @param d
	 * @return
	 */
	public static boolean lessDate(Date s, Date d)
	{
		if (s == null || d == null)
		{
			return false;
		}
		if (s.getTime() < d.getTime())
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 判断s日期小于等于d,是返回true否则返回false
	 * @param s
	 * @param d
	 * @return
	 */
	public static boolean lessEqualDate(Date s, Date d)
	{
		if (s == null || d == null)
		{
			return false;
		}
		if (s.getTime() <= d.getTime())
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args)
	{

	}
}
